package com.wxq.workpad.finishproduct

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.qianfan.qfcall.network.manager.HttpManager
import com.qianfan.qfcall.network.requestResponseThrowException
import com.sum.network.error.ApiException
import com.wxq.workpad.apiservice.PadApiService
import com.wxq.workpad.base.BaseFragment
import com.wxq.workpad.base.lifeRecycle
import com.wxq.workpad.databinding.FragmentListBinding
import com.wxq.workpad.entity.LaiLiaoItemEntity
import com.wxq.workpad.entity.ProductItemEntity
import com.wxq.workpad.finishproduct.adapter.ProductListAdapter
import com.wxq.workpad.utils.log.LogUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch



class ProductListFragment : BaseFragment<FragmentListBinding>() {

    private var status: String = ""
    private lateinit var mAdapter: ProductListAdapter
    private lateinit var mListData: MutableList<ProductItemEntity>

    var page: Int = 1;
    var oldPage: Int = 1;


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            status = it.getString("status").toString()

        }
    }

    override fun init() {
        //
        LogUtils.e("当前数据" + status)
        mAdapter = ProductListAdapter(mContext!!);
        binding.rvContent.apply {
            layoutManager = LinearLayoutManager(mContext!!, LinearLayoutManager.VERTICAL, false)
            adapter = mAdapter
        }
        // 调接口获取数据
        mListData = ArrayList()
        mAdapter.updateDate(mListData)
        mAdapter.setClickListener {
            showToast("点击了" + it.toString())

            ProductTestDetailActivity.naveToActivity(mContext!!, mListData.get(it).billNo)
        }
        // 下拉刷新
        binding.refreshLayout.setOnRefreshListener {
            oldPage = page
            page = 1
            getData()
        }
        // 加载更多
        binding.refreshLayout.setOnLoadMoreListener {
            oldPage = page
            page++
            getData()
        }
    }

    override fun lazyLoad() {
        super.lazyLoad()
        LogUtils.e("lazyLoad")
        showLoading()
        getData()
    }

    override fun onStart() {
        super.onStart()
        LogUtils.e("onStart")
    }

    override fun onResume() {
        super.onResume()
        LogUtils.e("onresume")
    }

    fun getData() {
        GlobalScope.launch(Dispatchers.Main) {
            try {
                var result = requestResponseThrowException {
                    HttpManager.create(PadApiService::class.java).getProductList(page.toString(), "5",status)
                }
                if (result != null && result.records != null) {
                    if (page == 1) {
                        mListData.clear()
                    }
                    mListData.addAll(result.records)
                    mAdapter.notifyDataSetChanged()
                }
            } catch (e: Exception) {
                page = oldPage

                if (e is ApiException) {
                    showToast(e.errMsg)
                } else {
                    showToast(e.toString())
                }
            }finally {
                binding.refreshLayout.finishRefresh()
                binding.refreshLayout.finishLoadMore()
                dismissLoading()
            }
        }.lifeRecycle(lifecycle)
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: String) =
            ProductListFragment().apply {
                arguments = Bundle().apply {
                    putString("status", param1)
                }
            }
    }
}